#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include "../include/MainHeader.h"



void create_Vfs(char vfs_label[30], long int size);
void test_read();
int main()

{	create_Vfs("VFS_23.dat",(1024*1024*100));
	test_read();	
	return 0;
}

void create_Vfs(char vfs_label[30], long int size)
{

	
	long int noOfBlocks;
	char block;
	
	//noOfBlocks=(size-40-(17* NO_OF_FILE_DESCRIPTOR))/1025;1006310

	noOfBlocks= ((size-42)/1042);//(30+8+4)+x(1+17+1024)=size

	long int nBlocks=noOfBlocks;
	
	printf("no of blocks in the file\n");	
	printf("\n%ld\n",noOfBlocks);
	

	FILE *fptr;
	long int i,j;
	
	
	//creating bit-array to keep track of free blocks
	bool fileBlock_bitArray[noOfBlocks];
	
	//Making an array of file descriptors
	
	struct FileDescriptor FileDescArray[noOfBlocks];

	for(i=0;i<noOfBlocks;i++)
		fileBlock_bitArray[i]=0; // initializing the bit array to '0'
	
	

	fptr=fopen(vfs_label,"w+b");
	
	if(fptr==NULL)
	{
		printf("Unable to create file!!");
	}

	else
	{
		fwrite(vfs_label,sizeof((char)vfs_label),30,fptr);
		
		fputc('$',fptr); //Delimiter
		fwrite(fileBlock_bitArray,sizeof((bool)fileBlock_bitArray),noOfBlocks,fptr);
		fputc('$',fptr);
		fwrite(&nBlocks,sizeof(nBlocks),1,fptr);
		fputc('$',fptr);
		fwrite(FileDescArray,sizeof(struct FileDescriptor),noOfBlocks,fptr);		
		fputc('$',fptr);

		for(j=0;j<noOfBlocks;j++)
		{
			fwrite(&block,(sizeof(block)),1024,fptr);
		}

		fclose(fptr);
	}
}








void test_read()
{
	FILE *f;
	f=fopen("VFS_23.dat","rb+");
	if(f==NULL)
		printf("unable to open file");
	else
	{
		char s[31],dol;
		bool b;
		long int nodesc;
		char c;
		fseek(f,30,SEEK_SET);
		

	//fread(&nodesc,sizeof(nodesc),1,f);
	fread(&c,sizeof(c),1,f);
	//fread(&c,sizeof(c),1,f);
	//	printf("%ld",nodesc);
		printf("%c",c);
		fclose(f);
	}	
}	

